<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML" 4.01"//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!-- Created: August 22, 2001-->
<html>
<head>
<title>Radiological Equipment Database</title>
<meta name="author" content="Eugene Mah">
<link rev="made" href="mailto:eugenem@ix.netcom.com">
<link rev="home" href="http://home.netcom.com/~eugenem/">
<meta name="description" content="">
<meta name="Keywords" content="">
<meta name="Reply-to" content="eugenem@ix.netcom.com">
<META http-equiv="PICS-Label" content='(PICS-1.1 "http://www.classify.org/safesurf/" l r (SS~~0001))'>
<?PHP
require('functions.inc');
require('dbc.inc');

?>
</head>

<body>
<h1>Radiological Equipment Database</h1>
<?PHP
if (isset($MachID) && empty($change)) {
  /*
    Machine has been selected.  Display a form showing the current machine
    info and text fields where changes can be entered
  */

  $query = "SELECT Modality,Machines.ModalityID,Description,Manufacturer,Machines.ManufacturerID,
            Model,SerialNumber,ManufDate,InstallDate,Location,Machines.LocationID,Room,Status,Notes 
			FROM Machines,Modality,Manufacturer,Location 
			WHERE MachineID=$MachID AND Machines.ModalityID=Modality.ModalityID 
			AND Machines.ManufacturerID=Manufacturer.ManufacturerID 
			AND Machines.LocationID=Location.LocationID";
  $result = mysql_query($query)
    or die ("<p>Unable to update status: $query</p>\n");

  $row = mysql_fetch_array($result);

  /*
    Get the list of contact people for this unit
  */
  $query = "SELECT Contacts2Machine.ContactPID,ContactPeople.Name 
            FROM Machines 
			LEFT JOIN Contacts2Machine USING (MachineID) 
			LEFT JOIN ContactPeople USING (ContactPID) 
			WHERE Machines.Status=\"Active\" AND Contacts2Machine.MachineID=$MachID";

  $contactresult = mysql_query($query);

  $contacts = mysql_query("select ContactPID,Name from ContactPeople order by Name");
  $modality = mysql_query("select * from Modality order by Modality");
  $manuf = mysql_query("select * from Manufacturer order by Manufacturer");
  $loc = mysql_query("select * from Location order by Location");
?>
<form action="MachineModify2.php" method="POST">
<input type="HIDDEN" name="change" value="1">
<input type="HIDDEN" name="MachID" value="<?PHP echo $MachID; ?>">
<p>
<!-- Machine info -->
<table border=1 cellpadding=3>
  <tr>
    <th>Modality</th><th>Description</th><th>Manufacturer</th><th>Model</th><th>Serial Number</th>
  </tr>
  <tr>
    <td><?PHP echo $row['Modality']; ?></td>
    <td><?PHP echo $row['Description']; ?></td>
    <td><?PHP echo $row['Manufacturer']; ?></td>
    <td><?PHP echo $row['Model']; ?></td>
    <td><?PHP echo $row['SerialNumber']; ?></td>
  </tr>
  <tr>
    <td><?PHP echo listbox_field($modality,"ModalityID",$row['ModalityID']); ?></td>
    <td><input type="TEXT" name="Description" size="25" value="<?PHP echo $row['Description']; ?>"></td>
    <td><?PHP echo listbox_field($manuf,"ManufacturerID",$row['ManufacturerID']); ?></td>
    <td><input type="TEXT" name="Model" size="20" value="<?PHP echo $row['Model']; ?>"></td>
    <td><input type="TEXT" name="SerialNumber" size="20" value="<?PHP echo $row['SerialNumber']; ?>"></td>
  </tr>
</table>
</p>
<p>
<table border=1 cellpadding=3>
  <tr>
    <th>Manufacture Date</th><th>Install Date</th><th>Location</th><th>Room</th><th>Status</th>
  </tr>
  <tr>
    <td><?PHP echo $row['ManufDate']; ?></td>
    <td><?PHP echo $row['InstallDate']; ?></td>
    <td><?PHP echo $row['Location']; ?></td>
    <td><?PHP echo $row['Room']; ?></td>
	<td><?PHP echo $row['Status']; ?></td>
  </tr>
  <tr>
    <td><input type="TEXT" name="ManufDate" size="10" value="<?PHP echo $row['ManufDate']; ?>"></td>
    <td><input type="TEXT" name="InstallDate" size="10" value="<?PHP echo $row['InstallDate']; ?>"></td>
    <td><?PHP echo listbox_field($loc,"LocationID",$row['LocationID']); ?></td>
    <td><input type="TEXT" name="Room" size="20" value="<?PHP echo $row['Room']; ?>"></td>
	<td>
	  <select name="Status" size="1">
        <option value="Active" selected="selected">Active</option>
        <option value="Inactive">Inactive</option>
        <option value="Removed">Removed</option>
      </select>
	</td>
  </tr>
</table>
</p>
<p>
<!-- Contact people -->
<table border=1 cellpadding=3>
<?PHP
  /*
    Count how many contacts for this unit were returned and set up
    that many header columns
  */
  $n = mysql_num_rows($contactresult);
  if ($n > 1) {  // More than 1 contact for this machine
    echo "<tr>\n";
    echo "  <th colspan=\"" . ($n + 1) . "\">Contact</th>\n";
    echo "</tr>\n";

    mysql_data_seek($contactresult,0);  // Reset contact results for this machine
    // Display each contact
    echo "<tr>\n";
    while ($contactrow = mysql_fetch_row($contactresult)) {
      echo "<td>" . $contactrow[1] . "</td>\n";
    }
	// Display an extra column in case another contact needs to be added
	echo "<td>&nbsp;</td>\n";
    echo "</tr>\n";

    // Display SELECT fields to change the contact person
    mysql_data_seek($contactresult,0);  // Reset contact results for this machine
    echo "<tr>\n";
    for ($i=1;$i<=$n;$i++) {
      echo "<td>";
      $contactrow = mysql_fetch_row($contactresult);
      echo listbox_field($contacts,"Contact[]",$contactrow[0]);
      echo "</td>\n";
      mysql_data_seek($contacts,0);  // Reset contact people list
    }
	// Display an extra select box
	echo "<td>";
	echo listbox_field($contacts,"Contact[]",0);
	echo "</td>\n";
    echo "</tr>\n";
  }
  else {  // No results in $contactresult.  No contacts for this unit, so display the whole list.
    echo "<tr><th>Contact</th><th>Contact</th></tr>\n";
    echo "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>\n";
    echo "<tr>\n";
    echo "<td>" . listbox_field($contacts,"Contact[]",0) . "</td>\n";
    mysql_data_seek($contacts,0);
    echo "<td>" . listbox_field($contacts,"Contact[]",0) . "</td>\n";
    echo "</tr>\n";
  }
?>
</table>
</p>
<!-- Notes for this machine-->
<p>
<table border=1 cellpadding=3>
  <tr>
    <th>Notes</th>
  </tr>
  <tr>
    <td><?PHP echo $row['Notes']; ?></td>
  </tr>
  <tr>
    <td><textarea name="Notes" rows="2" cols="70" value="<?PHP echo $row['Notes']; ?>"><?PHP echo $row['Notes']; ?></textarea>
  </tr>
</table>
</p>
<p><input type="SUBMIT" value="Modify"> / <input type="RESET" value="Reset"> / <a href="index.html" target="_top" >Main</a></p>
</form>
<?PHP
  mysql_free_result($contacts);
  mysql_free_result($contactresult);
  mysql_free_result($modality);
  mysql_free_result($manuf);
  mysql_free_result($loc);
  mysql_free_result($result);
}
else if (isset($MachID) && !empty($change)) {
  /*
    Step 2: Changes have been entered, now process them
  */
  $query = "update Machines set ";
  $query .= "ModalityID=$ModalityID, Description=\"$Description\", ManufacturerID=$ManufacturerID, ";
  $query .= "Model=\"$Model\", SerialNumber=\"$SerialNumber\", ManufDate=\"$ManufDate\", ";
  $query .= "InstallDate=\"$InstallDate\", LocationID=$LocationID, Status=\"$Status\", Room=\"$Room\", ";
  $query .= "Notes=\"$Notes\" where MachineID=$MachID";

  $result = mysql_query($query)
    or die ("<p>Unable to update: $query</p>\n");

  /*
    Insert changes to contacts table
    This is pretty kludgy
  */
  $query = "SELECT * FROM Contacts2Machine WHERE MachineID=$MachID";
  $n = mysql_num_rows(mysql_query($query));
  if ($n) {
    /*
      There are contacts already for this unit, so update them.
	  Delete the current contacts and re-insert the new ones.  Seems easier this way instead of trying to modify
	  the existing ones.
    */
	$query = "DELETE FROM Contacts2Machine WHERE MachineID=$MachID";
	$result = mysql_query($query);
	foreach ($Contact as $v) {
	  $query = "INSERT INTO Contacts2Machine (MachineID,ContactPID) VALUES ($MachID,$v)";
      $result = mysql_query($query)
        or die ("<p>Unable to insert: $query</p>\n");
	}
  } else {
    /*
      No contacts for this unit.  Need to insert them.
    */
	foreach ($Contact as $v) {
      $query = "INSERT INTO Contacts2Machine (MachineID,ContactPID) VALUES ($MachID,$v)";
      $result = mysql_query($query)
        or die ("<p>Unable to insert: $query</p>\n");
	}
  }
  echo "<h2>Record Updated</h2>\n";
  echo "<p>$Description updated</p>\n";
}
?>
<!-- Form to select a machine to modify -->
<form action="$_SERVER['PHP_SELF']" method="POST">
<P>Select a machine to modify
<?PHP
$machines = mysql_query("SELECT MachineID,Description FROM Machines WHERE Status=\"Active\" ORDER BY Description");
echo listbox_field($machines,"MachID","");
?>
</p>
<p><input type="SUBMIT" value="Select"> / <input type="RESET" value="Reset"> / <a href="index.html" target="_top" >Main</a></p>
</form>
<?PHP mysql_close() ?>

</body>
</html>
